package com.dswz.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtil {
	public static java.util.Date parseDate(String dateStr, String format) {
		java.util.Date date = null;
		try {
			DateFormat df = new SimpleDateFormat(format);
			date = df.parse(dateStr);
		} catch (Exception localException) {
		}
		return date;
	}
	public static java.util.Date parseDate(String dateStr) {
		return parseDate(dateStr.replaceAll("/", "-"), "yyyy-MM-dd");
	}

	public static java.util.Date parseDate(java.sql.Date date) {
		return date;
	}

	public static java.sql.Date parseSqlDate(java.util.Date date) {
		if (date != null) {
			return new java.sql.Date(date.getTime());
		}
		return null;
	}

	public static java.sql.Date parseSqlDate(String dateStr, String format) {
		java.util.Date date = parseDate(dateStr, format);
		return parseSqlDate(date);
	}

	public static java.sql.Date parseSqlDate(String dateStr) {
		return parseSqlDate(dateStr, "yyyy-MM-dd");
	}

	public static Timestamp parseTimestamp(String dateStr, String format) {
		java.util.Date date = parseDate(dateStr, format);
		if (date != null) {
			long t = date.getTime();
			return new Timestamp(t);
		}
		return null;
	}

	public static Timestamp parseTimestamp(String dateStr) {
		return parseTimestamp(dateStr, "yyyy-MM-dd HH:mm:ss");
	}

	public static String format(java.util.Date date, String format) {
		String result = "";
		try {
			if (date != null) {
				DateFormat df = new SimpleDateFormat(format);
				result = df.format(date);
			}
		} catch (Exception localException) {
		}
		return result;
	}

	public static String format(java.util.Date date) {
		return format(date, "yyyy-MM-dd");
	}

	public static int getYear(java.util.Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(1);
	}

	public static int getMonth(java.util.Date date) {
		/* 83 */ Calendar c = Calendar.getInstance();
		/* 84 */ c.setTime(date);
		/* 85 */ return c.get(2) + 1;
	}

	public static int getWeek(java.util.Date date) {
		/* 89 */ Calendar c = Calendar.getInstance();
		/* 90 */ c.setTime(date);
		/* 91 */ return c.get(7);
	}

	public static int getDay(java.util.Date date) {
		/* 95 */ Calendar c = Calendar.getInstance();
		/* 96 */ c.setTime(date);
		/* 97 */ return c.get(5);
	}

	public static int getHour(java.util.Date date) {
		/* 101 */ Calendar c = Calendar.getInstance();
		/* 102 */ c.setTime(date);
		/* 103 */ return c.get(11);
	}

	public static int getMinute(java.util.Date date) {
		/* 107 */ Calendar c = Calendar.getInstance();
		/* 108 */ c.setTime(date);
		/* 109 */ return c.get(12);
	}

	public static int getSecond(java.util.Date date) {
		/* 113 */ Calendar c = Calendar.getInstance();
		/* 114 */ c.setTime(date);
		/* 115 */ return c.get(13);
	}

	public static long getMillis(java.util.Date date) {
		/* 119 */ Calendar c = Calendar.getInstance();
		/* 120 */ c.setTime(date);
		/* 121 */ return c.getTimeInMillis();
	}

	public static String getDate(java.util.Date date) {
		/* 125 */ return format(date, "yyyy-MM-dd");
	}

	public static String getTime(java.util.Date date) {
		/* 129 */ return format(date, "HH:mm:ss");
	}

	public static String getDateTime(java.util.Date date) {
		/* 133 */ return format(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static java.util.Date addDate(java.util.Date date, int day) {
		/* 137 */ Calendar c = Calendar.getInstance();
		/* 138 */ c.setTimeInMillis(getMillis(date) + day * 24L * 3600L * 1000L);
		/* 139 */ return c.getTime();
	}

	public static java.util.Date addMinute(java.util.Date date, int minutes) {
		/* 143 */ Calendar c = Calendar.getInstance();
		/* 144 */ c.setTimeInMillis(getMillis(date) + minutes * 60L * 1000L);
		/* 145 */ return c.getTime();
	}

	public static int diffDate(java.util.Date date, java.util.Date date1) {
		/* 149 */ return (int) ((getMillis(date) - getMillis(date1)) / 86400000L);
	}

	public static int diffMinutes(java.util.Date date, java.util.Date date1) {
		/* 153 */ return (int) ((getMillis(date) - getMillis(date1)) / 60000L);
	}

	public static int getCurDate() {
		/* 157 */ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		/* 158 */ String date = format.format(new java.util.Date());
		/* 159 */ return Integer.parseInt(date);
	}

	public static int getCurTime() {
		/* 163 */ SimpleDateFormat format = new SimpleDateFormat("HHmmss");
		/* 164 */ String date = format.format(new java.util.Date());
		/* 165 */ return Integer.parseInt(date);
	}

	public static int getCurYear() {
		/* 169 */ Calendar c = Calendar.getInstance();
		/* 170 */ c.setTime(new java.util.Date());
		/* 171 */ return c.get(1);
	}
}
